﻿namespace Hidistro.UI.Web.Handler
{
    using Hidistro.ControlPanel.Commodities;
    using Hidistro.Core;
    using Hidistro.Entities;
    using Hidistro.SaleSystem.Comments;
    using Hidistro.UI.SaleSystem.Tags;
    using Hidistro.UI.Web;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Reflection;
    using System.Web;
    using Hidistro.Entities.Commodities;

    public class DesigComents : IHttpHandler
    {
        private string elementId = "";
        private string message = "";
        private string modeId = "";
        private string resultformat = "{{\"success\":{0},\"Result\":{1}}}";

        private bool CheckCommentArticle(JObject articleobject)
        {
            if (string.IsNullOrEmpty(articleobject["Title"].ToString()))
            {
                this.message = string.Format(this.resultformat, "false", "\"请输入文字标题!\"");
                return false;
            }
            if ((!string.IsNullOrEmpty(articleobject["MaxNum"].ToString()) && (Convert.ToInt16(articleobject["MaxNum"].ToString()) > 0)) && (Convert.ToInt16(articleobject["MaxNum"].ToString()) <= 100))
            {
                return true;
            }
            this.message = string.Format(this.resultformat, "false", "\"商品数量必须为1~100的正整数！\"");
            return false;
        }

        private bool CheckCommentAttribute(JObject attributeobject)
        {
            if (string.IsNullOrEmpty(attributeobject["CategoryId"].ToString()) || (Convert.ToInt16(attributeobject["CategoryId"].ToString()) <= 0))
            {
                this.message = string.Format(this.resultformat, "false", "\"请选择商品分类!\"");
                return false;
            }
            if (!string.IsNullOrEmpty(attributeobject["MaxNum"].ToString()) && (Convert.ToInt16(attributeobject["MaxNum"].ToString()) > 0))
            {
                return true;
            }
            this.message = string.Format(this.resultformat, "false", "\"商品数量必须为正整数！\"");
            return false;
        }

        private bool CheckCommentBrand(JObject brandobject)
        {
            if (!string.IsNullOrEmpty(brandobject["CategoryId"].ToString()) && (Convert.ToInt16(brandobject["CategoryId"].ToString()) <= 0))
            {
                this.message = string.Format(this.resultformat, "false", "\"请选择商品分类！\"");
                return false;
            }
            if (string.IsNullOrEmpty(brandobject["IsShowLogo"].ToString()) || string.IsNullOrEmpty(brandobject["IsShowTitle"].ToString()))
            {
                this.message = string.Format(this.resultformat, "false", "\"参数格式不正确!\"");
                return false;
            }
            if ((!string.IsNullOrEmpty(brandobject["MaxNum"].ToString()) && (Convert.ToInt16(brandobject["MaxNum"].ToString()) > 0)) && (Convert.ToInt16(brandobject["MaxNum"].ToString()) <= 100))
            {
                return true;
            }
            this.message = string.Format(this.resultformat, "false", "\"显示数量必须为0~100的正整数！\"");
            return false;
        }

        private bool CheckCommentCategory(JObject categoryobject)
        {
            if (string.IsNullOrEmpty(categoryobject["CategoryId"].ToString()) || (Convert.ToInt16(categoryobject["CategoryId"].ToString()) <= 0))
            {
                this.message = string.Format(this.resultformat, "false", "\"请选择商品分类!\"");
                return false;
            }
            if (!string.IsNullOrEmpty(categoryobject["MaxNum"].ToString()) && (Convert.ToInt16(categoryobject["MaxNum"].ToString()) > 0))
            {
                return true;
            }
            this.message = string.Format(this.resultformat, "false", "\"商品数量必须为正整数！\"");
            return false;
        }

        private bool CheckCommentKeyWord(JObject keywordobject)
        {
            if (!string.IsNullOrEmpty(keywordobject["CategoryId"].ToString()) && (Convert.ToInt16(keywordobject["CategoryId"].ToString()) <= 0))
            {
                this.message = string.Format(this.resultformat, "false", "\"请选择商品分类！\"");
                return false;
            }
            if ((!string.IsNullOrEmpty(keywordobject["MaxNum"].ToString()) && (Convert.ToInt16(keywordobject["MaxNum"].ToString()) > 0)) && (Convert.ToInt16(keywordobject["MaxNum"].ToString()) <= 100))
            {
                return true;
            }
            this.message = string.Format(this.resultformat, "false", "\"显示数量必须为1~100的正整数！\"");
            return false;
        }

        private bool CheckCommentMorelink(JObject morelinkobject)
        {
            if (!string.IsNullOrEmpty(morelinkobject["CategoryId"].ToString()) && (Convert.ToInt16(morelinkobject["CategoryId"].ToString()) <= 0))
            {
                this.message = string.Format(this.resultformat, "false", "\"请选择商品分类！\"");
                return false;
            }
            if (string.IsNullOrEmpty(morelinkobject["Title"].ToString()))
            {
                this.message = string.Format(this.resultformat, "false", "\"请输入链接标题！\"");
                return false;
            }
            return true;
        }

        private bool CheckCommentTitle(JObject titleobject)
        {
            if (string.IsNullOrEmpty(titleobject["Title"].ToString()) && string.IsNullOrEmpty(titleobject["ImageTitle"].ToString()))
            {
                this.message = string.Format(this.resultformat, "false", "\"请输入标题或上传图片！\"");
                return false;
            }
            return true;
        }

        private DataTable ConvertListToDataTable<T>(IList<T> list, params string[] propertyName)
        {
            List<string> list2 = new List<string>();
            if (propertyName != null)
            {
                list2.AddRange(propertyName);
            }
            DataTable table = new DataTable();
            if (list.Count > 0)
            {
                T local = list[0];
                PropertyInfo[] properties = local.GetType().GetProperties();
                foreach (PropertyInfo info in properties)
                {
                    if (list2.Count == 0)
                    {
                        table.Columns.Add(info.Name, info.PropertyType);
                    }
                    else if (list2.Contains(info.Name))
                    {
                        table.Columns.Add(info.Name, info.PropertyType);
                    }
                }
                for (int i = 0; i < list.Count; i++)
                {
                    ArrayList list3 = new ArrayList();
                    foreach (PropertyInfo info2 in properties)
                    {
                        if (list2.Count == 0)
                        {
                            object obj2 = info2.GetValue(list[i], null);
                            list3.Add(obj2);
                        }
                        else if (list2.Contains(info2.Name))
                        {
                            object obj3 = info2.GetValue(list[i], null);
                            list3.Add(obj3);
                        }
                    }
                    object[] values = list3.ToArray();
                    table.LoadDataRow(values, true);
                }
            }
            return table;
        }

        private string GetCategorys()
        {
            DataTable table = null;
            string str = "";
            string[] propertyName = new string[] { "CategoryId", "Name", "Depth" };
            table = this.ConvertListToDataTable<CategoryInfo>(CatalogHelper.GetSequenceCategories(), propertyName);
            if (table != null)
            {
                str = JsonConvert.SerializeObject(table, new JsonConverter[] { new ConvertTojson() });
            }
            return str;
        }

        private string GetMainArticleCategories()
        {
            return JsonConvert.SerializeObject(CommentBrowser.GetArticleMainCategories());
        }

        public Dictionary<string, string> GetXmlNodeString(JObject scriptobject)
        {
            Dictionary<string, string> dictionary = scriptobject.ToObject<Dictionary<string, string>>();
            Dictionary<string, string> dictionary2 = new Dictionary<string, string>();
            foreach (KeyValuePair<string, string> pair in dictionary)
            {
                dictionary2.Add(pair.Key, Globals.HtmlEncode(Globals.HtmlEncode(pair.Value.ToString())));
            }
            return dictionary2;
        }

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                this.modeId = context.Request.Form["ModelId"];
                string key;
                switch (key = this.modeId)
                {
                    case "commentarticleview":
                        {
                            string arg = this.GetMainArticleCategories();
                            this.message = string.Format(this.resultformat, "true", arg);
                            break;
                        }
                    case "commentCategory":
                        {
                            string arg = this.GetCategorys();
                            this.message = string.Format(this.resultformat, "true", arg);
                            break;
                        }
                    case "editecommentarticle":
                        {
                            string value = context.Request.Form["Param"];
                            if (!string.IsNullOrEmpty(value))
                            {
                                JObject articleobject = (JObject)JsonConvert.DeserializeObject(value);
                                if (this.CheckCommentArticle(articleobject) && this.UpdateCommentArticle(articleobject))
                                {
                                    var value2 = new
                                    {
                                        ComArticle = new Common_SubjectArticle
                                        {
                                            CommentId = Convert.ToInt32(this.elementId)
                                        }.RendHtml()
                                    };
                                    this.message = string.Format(this.resultformat, "true", JsonConvert.SerializeObject(value2));
                                }
                            }
                            break;
                        }
                    case "editecommentcategory":
                        {
                            string value3 = context.Request.Form["Param"];
                            if (!string.IsNullOrEmpty(value3))
                            {
                                JObject categoryobject = (JObject)JsonConvert.DeserializeObject(value3);
                                if (this.CheckCommentCategory(categoryobject) && this.UpdateCommentCategory(categoryobject))
                                {
                                    var value4 = new
                                    {
                                        ComCategory = new Common_SubjectCategory
                                        {
                                            CommentId = Convert.ToInt32(this.elementId)
                                        }.RendHtml()
                                    };
                                    this.message = string.Format(this.resultformat, "true", JsonConvert.SerializeObject(value4));
                                }
                            }
                            break;
                        }
                    case "editecommentbrand":
                        {
                            string value5 = context.Request.Form["Param"];
                            if (!string.IsNullOrEmpty(value5))
                            {
                                JObject jObject = (JObject)JsonConvert.DeserializeObject(value5);
                                if (this.CheckCommentBrand(jObject) && this.UpdateCommentBrand(jObject))
                                {
                                    var value6 = new
                                    {
                                        ComBrand = new Common_SubjectBrand
                                        {
                                            CommentId = Convert.ToInt32(this.elementId)
                                        }.RendHtml()
                                    };
                                    this.message = string.Format(this.resultformat, "true", JsonConvert.SerializeObject(value6));
                                }
                            }
                            break;
                        }
                    case "editecommentkeyword":
                        {
                            string value7 = context.Request.Form["Param"];
                            if (!string.IsNullOrEmpty(value7))
                            {
                                JObject keywordobject = (JObject)JsonConvert.DeserializeObject(value7);
                                if (this.CheckCommentKeyWord(keywordobject) && this.UpdateCommentKeyWord(keywordobject))
                                {
                                    var value8 = new
                                    {
                                        ComCategory = new Common_SubjectKeyword
                                        {
                                            MaxNum = Convert.ToInt32(this.elementId)
                                        }.RendHtml()
                                    };
                                    this.message = string.Format(this.resultformat, "true", JsonConvert.SerializeObject(value8));
                                }
                            }
                            break;
                        }
                    case "editecommentattribute":
                        {
                            string value9 = context.Request.Form["Param"];
                            if (!string.IsNullOrEmpty(value9))
                            {
                                JObject attributeobject = (JObject)JsonConvert.DeserializeObject(value9);
                                if (this.CheckCommentAttribute(attributeobject) && this.UpdateCommentAttribute(attributeobject))
                                {
                                    var value10 = new
                                    {
                                        ComAttribute = new Common_SubjectAttribute
                                        {
                                            CommentId = Convert.ToInt32(this.elementId)
                                        }.RendHtml()
                                    };
                                    this.message = string.Format(this.resultformat, "true", JsonConvert.SerializeObject(value10));
                                }
                            }
                            break;
                        }
                    case "editecommenttitle":
                        {
                            string value11 = context.Request.Form["Param"];
                            if (!string.IsNullOrEmpty(value11))
                            {
                                JObject titleobject = (JObject)JsonConvert.DeserializeObject(value11);
                                if (this.CheckCommentTitle(titleobject) && this.UpdateCommentTitle(titleobject))
                                {
                                    var value12 = new
                                    {
                                        ComTitle = new Common_SubjectTitle
                                        {
                                            CommentId = Convert.ToInt32(this.elementId)
                                        }.RendHtml()
                                    };
                                    this.message = string.Format(this.resultformat, "true", JsonConvert.SerializeObject(value12));
                                }
                            }
                            break;
                        }
                    case "editecommentmorelink":
                        {
                            string value13 = context.Request.Form["Param"];
                            if (!string.IsNullOrEmpty(value13))
                            {
                                JObject morelinkobject = (JObject)JsonConvert.DeserializeObject(value13);
                                if (this.CheckCommentMorelink(morelinkobject) && this.UpdateMorelink(morelinkobject))
                                {
                                    var value14 = new
                                    {
                                        ComMoreLink = new Common_SubjectMoreLink
                                        {
                                            CommentId = Convert.ToInt32(this.elementId)
                                        }.RendHtml()
                                    };
                                    this.message = string.Format(this.resultformat, "true", JsonConvert.SerializeObject(value14));
                                }
                            }
                            break;
                        }
                }
            }
            catch (Exception ex)
            {
                this.message = "{\"success\":false,\"Result\":\"未知错误:" + ex.Message + "\"}";
            }
            context.Response.ContentType = "text/json";
            context.Response.Write(this.message);
        }

        private bool UpdateCommentArticle(JObject articleobject)
        {
            this.message = string.Format(this.resultformat, "false", "\"修改文章标签列表失败\"");
            this.elementId = articleobject["Id"].ToString().Split(new char[] { '_' })[1];
            articleobject["Id"] = this.elementId;
            Dictionary<string, string> xmlNodeString = this.GetXmlNodeString(articleobject);
            return TagsHelper.UpdateCommentNode(Convert.ToInt16(this.elementId), "article", xmlNodeString);
        }

        private bool UpdateCommentAttribute(JObject attributeobject)
        {
            this.message = string.Format(this.resultformat, "false", "\"修改属性标签列表失败\"");
            this.elementId = attributeobject["Id"].ToString().Split(new char[] { '_' })[1];
            attributeobject["Id"] = this.elementId;
            Dictionary<string, string> xmlNodeString = this.GetXmlNodeString(attributeobject);
            return TagsHelper.UpdateCommentNode(Convert.ToInt16(this.elementId), "attribute", xmlNodeString);
        }

        private bool UpdateCommentBrand(JObject attributeobject)
        {
            this.message = string.Format(this.resultformat, "false", "\"修改品牌标签列表失败\"");
            this.elementId = attributeobject["Id"].ToString().Split(new char[] { '_' })[1];
            attributeobject["Id"] = this.elementId;
            Dictionary<string, string> xmlNodeString = this.GetXmlNodeString(attributeobject);
            return TagsHelper.UpdateCommentNode(Convert.ToInt16(this.elementId), "brand", xmlNodeString);
        }

        private bool UpdateCommentCategory(JObject categoryobject)
        {
            this.message = string.Format(this.resultformat, "false", "\"修改分类标签列表失败\"");
            this.elementId = categoryobject["Id"].ToString().Split(new char[] { '_' })[1];
            categoryobject["Id"] = this.elementId;
            Dictionary<string, string> xmlNodeString = this.GetXmlNodeString(categoryobject);
            return TagsHelper.UpdateCommentNode(Convert.ToInt16(this.elementId), "category", xmlNodeString);
        }

        private bool UpdateCommentKeyWord(JObject keywordobject)
        {
            this.message = string.Format(this.resultformat, "false", "\"修改品牌标签列表失败\"");
            this.elementId = keywordobject["Id"].ToString().Split(new char[] { '_' })[1];
            keywordobject["Id"] = this.elementId;
            Dictionary<string, string> xmlNodeString = this.GetXmlNodeString(keywordobject);
            return TagsHelper.UpdateCommentNode(Convert.ToInt16(this.elementId), "keyword", xmlNodeString);
        }

        private bool UpdateCommentTitle(JObject titleobject)
        {
            this.message = string.Format(this.resultformat, "false", "\"修改品牌标签列表失败\"");
            this.elementId = titleobject["Id"].ToString().Split(new char[] { '_' })[1];
            titleobject["Id"] = this.elementId;
            Dictionary<string, string> xmlNodeString = this.GetXmlNodeString(titleobject);
            return TagsHelper.UpdateCommentNode(Convert.ToInt16(this.elementId), "title", xmlNodeString);
        }

        private bool UpdateMorelink(JObject morelinkobject)
        {
            this.message = string.Format(this.resultformat, "false", "\"修改品牌标签列表失败\"");
            this.elementId = morelinkobject["Id"].ToString().Split(new char[] { '_' })[1];
            morelinkobject["Id"] = this.elementId;
            Dictionary<string, string> xmlNodeString = this.GetXmlNodeString(morelinkobject);
            return TagsHelper.UpdateCommentNode(Convert.ToInt16(this.elementId), "morelink", xmlNodeString);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

